You've learned about if and else, and how they control what your program does. Here's a quick refresher on the syntax:
You've learned about if and else, and how they control what your program does. Here's a quick refresher on the syntax:
Write an if / else statement inside the isEven function. It should returntrue; if the number it receives is evenly divisible by 2. Otherwise (else), it should return false;.
Make sure to return - don't useconsole.log()!
Good! Let's also get some practice in with else if, as well as learn about a fancy new function: isNaN.
If you call isNaN on something, it checks to see if that thing is not a number. So:
Be careful: if you call isNaN on a string that looks like a number, like'42', JavaScript will try to help by automatically converting the string'42' to the number 42 and returnfalse (since 42 is a number).
Note that you can't just do
unicorns. You can, however, doelse if branch to your existing if/else statement. If thenumber put into the function is not a number at all, instead of returntrue; or return false;, the function should return a string that tells the user that their input isn't a number. (This string can say whatever you like.)As you might imagine, if you have a lotof choices you want to cover in a program, it might be annoying to typeelse if () ten times. That's why JavaScript has the switch statement!
switch allows you to preset a number of options (called cases), then check an expression to see if it matches any of them. If there's a match, the program will perform the action for the matching case; if there's no match, it can execute a default option.
The switch statement is put together like this:
switch()parentheses to each case. It will run the code below each case if it finds a match, and will execute the defaultcode if no match is found.Our switch statement needs a casefor 'yellow'. Add it in and make it log a string of your choice to the console (it should be different from the defaultstring).
Don't forget to end your case with abreak statement—otherwise, it will go on and execute the code for default, too! We don't want that.
Now that you've added cases to an existing switch, let's practice adding adefault block.
default block at the bottom of the switch statement, then run the code a few times with different inputs.switch: super useful!You know what they say: practice makes perfect!
switch statement. Complete the existing case, then add at least one additional case and a defaultbehavior with whateverconsole.log() calls you like.Well done! Even though we've been focusing on switch, we've covered a lot so far. You've:
if/else if/elsefor and whileswitchstatement and how to use it instead of multiple if/elsesswitch syntaxswitch!So far we've seen how to control our programs given a single condition: whether one variable is equal to a certain value, for instance. But what if we want to check more than one variable?
For this, we'll need logical operators. JavaScript has three: and (&&), or(||), and not (!).
Using these, we can check several variables at once! Check out the code in the editor.
The logical operator and is written in JavaScript like this: &&. It evaluates totrue when both expressions aretrue; if they're not, it evaluates tofalse.
hungry andfoodHere, and set them both equal totrue. Inside the eat function, create an if statement that returns trueonly if both hungry and foodHere aretrue, and false otherwise. The function eat should take no input andhungry and foodHere should both be globals.The logical operator or is written in JavaScript like this: ||. It evaluates totrue when one or the other or bothexpressions are true; if they're not, it evaluates to false.
||. The vertical bar character is located right above the Enter key on your keyboard.tired andbored, and set one equal to true and the other equal to false. (It doesn't matter which is which.) Inside the napfunction, create an if statement that returns true if either tired orbored (or both!) are true, and falseotherwise.The logical operator not is written in JavaScript like this: !. It makes trueexpressions false, and vice-versa.
programmingand set it to false. Then, write an if/else statement inside happy so thathappy returns true if programming isfalse and false otherwise.Well done! That last one was particularly tricky.
In this lesson you:
switchstatement&&), or (||), and not (!)